package ca.tatham.senssim.diffusion;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import ca.tatham.senssim.SensorType;
import ca.tatham.senssim.Simulation;
import ca.tatham.senssim.util.Address;
import ca.tatham.senssim.util.Location;
import ca.tatham.senssim.util.Region;

public abstract class AbstractSinkNode extends AbstractDiffusionNode
{
  public static final SensorType SENSOR_TYPE = SensorType.SAMPLE;

  private final int m_sendInterval = 60000;

  protected final static Region REGION = new Region(new Location(4, 3),
      new Location(7, 8));
  private final Set<DataMessage> m_receivedData = new HashSet<DataMessage>();

  public AbstractSinkNode(Address address)
  {
    super(address, Integer.MAX_VALUE);
  }

  public Collection<DataMessage> getReceivedData()
  {
    return m_receivedData;
  }

  @Override
  public boolean isSource()
  {
    return false;
  }

  @Override
  public boolean isSink()
  {
    return true;
  }

  @Override
  protected void processInterestMessage(InterestMessage packet)
  {
    // do nothing
  }

  public void tick()
  {
    if (Simulation.getTime() % m_sendInterval == 0)
    {
      sendPeriodicInterest();
    }
  }

  protected abstract void sendPeriodicInterest();

  /**
   * @param message
   * @return true if the message was not already cached
   * @see Set#add(Object)
   */
  @Override
  protected final boolean saveData(DataMessage message)
  {
    message.received();
    return m_receivedData.add(message);
  }

  public char getOpenSymbol()
  {
    return '*';
  }

  public char getCloseSymbol()
  {
    return '*';
  }

}
